package com.example.novelsys.interceptor;

import com.example.novelsys.entity.Users;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyLoginInterceptor implements HandlerInterceptor {


    //登录认证
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {



        //获取url，如果访问  会员中心，上传电子书，收藏，点赞等，不能直接访问，需要登录认证
        String path= request.getServletPath();

        if("/toMemberInfo".equals(path)){

            //判断session是否存在登录的用户信息
            HttpSession session =request.getSession();
            Users user =(Users) session.getAttribute("user");

            if(user==null){
                System.out.println("--------------------------------");
                //未登录，跳转登录页
                request.getRequestDispatcher("toLogonRegister").forward(request,response);
                return  false;
            }
            System.out.println("*********************************");
        }


        //用户登录过，可以继续向controller流去
        return true;
    }
}
